# Ensure that the value of "$key" in "$file" is "$value"
# assumes that "$key" is at *beginning* of the line
# whitespace around delimiter is ignored ("$key$delim$value" == "$key $delim $value")

# This handles syntax differences between BSD sed and GNU sed
$sed_extension = $operatingsystem ? {
  Darwin  => "''",
  Debian  => "",
  default => "",
}

define ensure_key_value($file, $key, $value, $delimiter = " ") {
  # Append line if "$key" not in "$file"
  exec { "echo '$key$delimiter$value' >> $file":
    unless => "grep -qe '^$key[[:space:]]*$delimiter' -- '$file'",
    path   => "/bin:/usr/bin"
  }

  # Update it if it already exists...
  exec { "sed -i $sed_extension 's/^$key$delimiter.*$/$key$delimiter$value/g' '$file'":
    unless => "grep -xqe '$key[[:space:]]*$delimiter[[:space:]]*$value' -- '$file'",
    path   => "/bin:/usr/bin"
  }
}
